<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header width="200px">
      <!-- 左上角图片和项目名称 -->
      <div>
        <!-- 图片 -->
        <img src="../assets/heima.png" alt="">
        <!-- 项目名称 -->
        <span>电商后台管理系统</span>
      </div>
      <!-- 退出按钮 -->
      <el-button type="info" @click="signOut">退出</el-button>
    </el-header>
    <!-- 侧边栏和主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="toggle ? '64px' : '200px'">
        <!-- 展开功能 -->
        <div class="toggle-button"
        @click="toggleCollapse">
          <i>|||</i>
        </div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
        background-color="#333744"
        text-color="#fff"
        active-text-color="#409EFF"
        unique-opened
        router
        :collapse="toggle"
        :collapse-transition="false"
        :default-active="activeName">
          <!-- 一级菜单区域 -->
          <el-submenu :index="item.id + ''"
          v-for="item in menuList"
          :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item :index="'/' + it.path"
            v-for="it in item.children"
            :key="it.id"
            @click="saveNavStage('/' + it.path)">
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{it.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <!-- 主体组件占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 动态图标对象
      icons: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制是否展开左侧菜单
      toggle: false,
      // 保存在sessionStroge中的链接地址
      activeName: ''
    }
  },
  // 生命周期钩子函数
  created() {
    this.getMenuList()
    this.activeName = window.sessionStorage.getItem('activeName')
  },
  methods: {
    // 点击按钮退出home页
    signOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      console.log(res)
      this.menuList = res.data
    },
    // 控制左侧菜单展开和隐藏事件
    toggleCollapse() {
      this.toggle = !this.toggle
    },
    // 保存链接激活状态
    saveNavStage(activeName) {
      window.sessionStorage.setItem('activeName', activeName)
      this.activeName = activeName
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  background-color: #373d41;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  font-size: 20px;
  color: #fff;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    width: 100%;
  }
}
.toggle-button {
  display: flex;
  justify-content: center;
  height: 24px;
  line-height: 24px;
  background-color: #4A5064;
  font-size: 10px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
  i {
    font-style: normal;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
</style>
